package cn.emailChat.ai.consumer.util;



import cn.emailChat.ai.dto.MemoryContext;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
 * @Description 用于拼装“记忆+原文”
 * @Author susu
 * @Date 2025/8/20
 */
public final class PromptComposer {
    private PromptComposer() {}

    public static String compose(MemoryContext ctx, String subject, String userText) {
        StringBuilder sb = new StringBuilder(1024);
        sb.append("【系统说明】你是公司的邮件助手。保持专业、简洁、有礼貌。若上下文已有承诺/价格/日期，必须保持一致，除非说明变更原因。\n\n");

        if (notEmpty(ctx.getConversationSummary())) {
            sb.append("【线程摘要】（更新于 ").append(nowDate()).append("）\n")
                    .append(ctx.getConversationSummary()).append("\n\n");
        }
        if (notEmpty(ctx.getContactProfileSummary())) {
            sb.append("【联系人画像】（跨线程）\n")
                    .append(ctx.getContactProfileSummary()).append("\n\n");
        }

        List<String> turns = ctx.getRecentTurns();
        if (turns != null && !turns.isEmpty()) {
            sb.append("【最近片段】（新→旧，已脱敏、压缩）\n");
            int i = 1;
            for (String t : turns) {
                sb.append(i++).append(") ").append(t).append("\n");
            }
            sb.append("\n");
        }

        sb.append("【当前主题】").append(subject == null ? "" : subject).append("\n\n");
        sb.append("【用户来信】\n").append(userText == null ? "" : userText.trim()).append("\n");
        sb.append("\n【你的任务】请基于以上上下文直接生成给用户的回信正文。");
        return sb.toString();
    }

    private static boolean notEmpty(String s) { return s != null && !s.isBlank(); }
    private static String nowDate() { return LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE); }
}
